package pe.candyncoffee.mytrace.photo;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;

public class PhotoUtil {

	public static boolean saveBitmap(final Context context, Bitmap bitmap, String destFilename){
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(destFilename);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		}
		if (bitmap != null)
			return bitmap.compress(CompressFormat.JPEG, 100, fos);
		return false;
	}
	
	public static byte[] bitmapToBytes(Bitmap bitmap){
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		bitmap.compress(CompressFormat.JPEG, 100, os);
		return os.toByteArray();
	}
	public static Bitmap bytesToBitmap(byte[] array){
		if (array == null || array.length == 0) return null;
		return BitmapFactory.decodeByteArray(array, 0, array.length);
	}
}